from rest_framework.serializers import ModelSerializer
from user.models import User
from question.models import Question,Reply,Label
from article.models import Article
from recruit.models import Enterprise,Recruit
from rest_framework import serializers

class ArticleSimpSerializer(ModelSerializer):

    user = serializers.CharField(read_only=True)

    class Meta:
        model = Article
        fields = '__all__'

class RecruitSerializer(ModelSerializer):

    class Meta:
        model = Recruit
        fields = '__all__'

class EnterpriseSimpleSerializer(ModelSerializer):

    recruits = serializers.SerializerMethodField()
    class Meta:
        model = Enterprise
        fields = ['id','name','labels','logo','recruits','summary']

    def get_recruits(self,obj):
        recruits = Recruit.objects.filter(id=obj.id)
        recruit_list = [ ]
        for recruit in recruits:
            recruit_list.append(recruit.id)
        # ser_recruit = RecruitSerializer(instance=recruits,many=True)
        count = len(recruit_list)
        return count

class QuestionSimpleSerializer(ModelSerializer):

    class Meta:
        model = Question
        fields = '__all__'

class AnswerQuesSimpleSerializer(ModelSerializer):

    user = serializers.PrimaryKeyRelatedField(read_only=True)
    content = serializers.StringRelatedField(read_only=True)
    class Meta:
        model = Reply
        fields = ['id','content','useful_count','createtime','problem','user','type']

class ArticleSimpleSerialzer(ModelSerializer):

    user = serializers.CharField(read_only=True)
    class Meta:
        model = Article
        fields = '__all__'

class LabelSimpleSerializer(ModelSerializer):

    class Meta:
        model = Label
        fields = '__all__'

class UserInfoSerializer(ModelSerializer):

    enterprises = serializers.SerializerMethodField()
    # enterprises = EnterpriseSimpleSerializer(many=True)
    questions = QuestionSimpleSerializer(many=True)
    collected_articles = serializers.SerializerMethodField()
    answer_question = serializers.SerializerMethodField()
    labels = LabelSimpleSerializer(many=True)
    username = serializers.CharField(read_only=True)


    class Meta:
        model = User
        fields = '__all__'
        # fields = [
                # 'id','username','mobile','realname','birthday',
                # 'sex','avatar','website','email','city','address',
                # 'labels','questions','answer_question','collected_articles',
                # 'enterprises'
                #   ]
        extra_kwargs = {
            'password':{'write_only':True}
        }

    def get_enterprises(self,obj):
        enterprise = Enterprise.objects.filter(users=obj)
        ser_enterprise = EnterpriseSimpleSerializer(instance=enterprise,many=True)
        return ser_enterprise.data

    def get_collected_articles(self,obj):
        articles = Article.objects.filter(user=obj)
        ser_articles = ArticleSimpleSerialzer(instance=articles,many=True)
        return ser_articles.data

    def get_answer_question(self,obj):
        aq = Reply.objects.filter(user=obj)
        ser_aq = AnswerQuesSimpleSerializer(instance=aq,many=True)
        return ser_aq.data